package com.linghang.wusthelper.spider.library;

import com.linghang.wusthelper.entity.primary.BookCollectionInfo;
import com.linghang.wusthelper.entity.primary.BookInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author origin
 * 图书信息爬虫
 */
public class LibBookInfoSpider implements PageProcessor {

    private BookInfo bookInfo;

    private String lock;

    private Logger log = LoggerFactory.getLogger(getClass());

    private Site site = Site.me().setTimeOut(10000).setRetryTimes(3)
            .setDomain("http://www.lib.wust.edu.cn")
            .addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
            .addHeader("Accept-Encoding","gzip,deflate,sdch")
            .addHeader("Accept-Language", "zh-CN,zh;q=0.8")
            .addHeader("Connection", "keep-alive")
            .addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36");

    public LibBookInfoSpider(BookInfo bookInfo,String lock){
        this.bookInfo = bookInfo;
        this.lock = lock;
    }

    @Override
    public void process(Page page) {

        String name = page.getHtml().xpath("//*[@id=\"item_detail\"]/dl[1]/dd/a/text()").get();
        String auth = page.getHtml().xpath("//*[@id=\"item_detail\"]/dl[1]/dd/text()").get();

        String dlpath = "//*[@id=\"item_detail\"]/dl[2]";
        String publisher = "";
        String ISBN = "";
        String introduction = "";
        int j = 2;

        //  逐行获取内容
        while (page.getHtml().xpath(dlpath).get() != null){
            if (page.getHtml().xpath(dlpath+"/dt/text()").get().equals("出版发行项:")){
                publisher += page.getHtml().xpath(dlpath+"/dd/text()").get();
            }
            if (page.getHtml().xpath(dlpath+"/dt/text()").get().equals("ISBN及定价:")){
                ISBN += page.getHtml().xpath(dlpath+"/dd/text()").get();
            }
            if (page.getHtml().xpath(dlpath+"/dt/text()").get().equals("提要文摘附注:")){
                introduction += page.getHtml().xpath(dlpath+"/dd/text()").get();
            }

            j++;
            dlpath = "//*[@id=\"item_detail\"]/dl["+j+"]";
        }

        String imageUrl = "https://www.yuntu.io/book/cover?isbn="+ISBN;

        //  获取馆藏信息
        List<BookCollectionInfo> collectionInfoList = new ArrayList<>();
        String path = "//*[@id=\"item\"]/tbody/tr[2]";
        int i = 2;
        while (page.getHtml().xpath(path).get() != null){
            String bookSearchNum = page.getHtml().xpath(path+"/td[1]/text()").get();
            String bookCode = page.getHtml().xpath(path+"/td[2]/text()").get();
            String collectionPlace = page.getHtml().xpath(path+"/td[4]/text()").get().replaceAll("     ","");
            String statu = page.getHtml().xpath(path+"/td[5]/font/text()").get();
            if (statu == null){
                statu = page.getHtml().xpath(path+"/td[5]/text()").get();
            }

            BookCollectionInfo bookCollectionInfo = new BookCollectionInfo();
            bookCollectionInfo.setBookSearchNum(bookSearchNum);
            bookCollectionInfo.setBookCode(bookCode);
            bookCollectionInfo.setCollectionPlace(collectionPlace);
            bookCollectionInfo.setStatu(statu);

            collectionInfoList.add(bookCollectionInfo);
            i++;
            path = "//*[@id=\"item\"]/tbody/tr["+i+"]";
        }
        bookInfo.setBookNameAndAuth(name+auth);
        bookInfo.setPublisher(publisher);
        bookInfo.setISBN(ISBN);
        bookInfo.setIntroduction(introduction);
        bookInfo.setImageUrl(imageUrl);
        bookInfo.setCollectionInfoList(collectionInfoList);
        log.info("1");
        synchronized (lock){
            lock.notify();
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}
